package com.example.chessmigame;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

public class GameActivity3 extends AppCompatActivity {
    public static final String TAG = "Game3";
    public static final int UPDATE = 0x1;
    int a,b,c,gt,n3;
    int count;
    String d;
    EditText time;
    TextView num,c3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game3);

        SharedPreferences setting = getSharedPreferences("GameSettings",MODE_PRIVATE);
        String countdown = setting.getString("countdown","5");
        String n3_str = setting.getString("n3","2");
        Log.i(TAG,"获取预设值");

        gt = Integer.parseInt(countdown);
        n3 = Integer.parseInt(n3_str);

        a = (int) (Math.random() * Math.pow(10,n3));
        Log.i(TAG,"生成随机数"+a);

        b = (int) (Math.random() * Math.pow(10,n3));
        Log.i(TAG,"生成随机数"+b);

        c = a*b;

        Log.i(TAG, "答案" +c);

        d = a+" x "+b;

        num=findViewById(R.id.output3);
        num.setText(d);

        Intent intent1 = getIntent();
        String count_str= intent1.getStringExtra("count");
        String q;
        if (count_str == null) {
            q = "0";
        } else {
            q = count_str;
        }
        c3 =  findViewById(R.id.c3);
        c3.setText(q+"分");

        time = findViewById(R.id.time3);
        EditText inputnum = findViewById(R.id.input3);
        inputnum.setFocusableInTouchMode(false);
        Log.i(TAG,"不可触碰");

        Log.i(TAG,"成功创建页面");

        begin();
        Log.i(TAG,"成功开始倒计时");
    }

    public void submit3(View V) {
        TextView inputnum=(TextView)findViewById(R.id.input3);
        String inputText= inputnum.getText().toString();

        if(inputText.equals("")){
            Toast toastCenter = Toast.makeText(getApplicationContext(),"请输入！",Toast.LENGTH_LONG);
            toastCenter.setGravity(Gravity.CENTER,0,0);
            toastCenter.show();
        }else{
            int inputnumber = Integer.parseInt(inputText);
            Log.i(TAG,"随机数"+a+",输入数"+inputnumber);

            if(inputnumber == c){//答对了
                Intent intent1 = getIntent();
                String count_str= intent1.getStringExtra("count");
                Log.i(TAG,"答对了");
                if(count_str != null){//不是第一次答对
                    count = Integer.parseInt(count_str)+1;
                    String count_str1 = String.valueOf(count);
                    Intent intent2 = new Intent(GameActivity3.this, GameActivity3.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent2.putExtra("count",count_str1);
                    startActivity(intent2);
                }else{//第一次答对
                    count = 1;
                    String count_str1 = String.valueOf(count);
                    Intent intent2 = new Intent(GameActivity3.this, GameActivity3.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent2.putExtra("count",count_str1);
                    startActivity(intent2);
                }
            }else{//答错了
                Intent intent1 = getIntent();
                String count_str= intent1.getStringExtra("count");
                Log.i(TAG,"count_str::"+count_str);
                if(count_str != null){//不是在第一次游戏时答错
                    count = Integer.parseInt(count_str);
                    String count_str1 = String.valueOf(count);
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("游戏结果")
                            .setMessage("您的答案是："+inputText+",正确答案是："+c+";您的分数为："+count_str1)
                            .setPositiveButton("返回游戏选择页面", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent=new Intent(GameActivity3.this,GameActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent);
                                }
                            });
                    builder.setNegativeButton("再来一次",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent=new Intent(GameActivity3.this,GameActivity3.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    });
                    builder.create().show();
                }else{//第一次答题就错了
                    count = 0;
                    String count_str1 = String.valueOf(count);
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("游戏结果")
                            .setMessage("您的答案是："+inputText+",正确答案是："+c+";您的分数为："+count_str1)
                            .setPositiveButton("返回游戏选择页面", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent=new Intent(GameActivity3.this,GameActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent);
                                }
                            });
                    builder.setNegativeButton("再来一次",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent=new Intent(GameActivity3.this,GameActivity3.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    });
                    builder.create().show();
                }
            }
        }
    }

    public void over(View v){
        Intent intent1 = getIntent();
        String count_str= intent1.getStringExtra("count");
        String a;
        if (count_str == null) {
            a = "0";
        } else {
            a = count_str;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("游戏结果")
                .setMessage("现在您的分数为："+a)
                .setPositiveButton("返回游戏选择页面", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent=new Intent(GameActivity3.this,GameActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }
                });
        builder.setNegativeButton("再来一次",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent=new Intent(GameActivity3.this,GameActivity3.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        builder.create().show();
    }

    private static class MyHandler extends Handler {
        //弱引用
        WeakReference<GameActivity3> weakReference;
        public MyHandler(GameActivity3 activity) {
            weakReference = new WeakReference<GameActivity3>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            GameActivity3 activity = weakReference.get();
            if (activity != null) {
                activity.time.setText(String.valueOf(msg.arg1));
            }
        }
    }

    private GameActivity3.MyHandler handler = new GameActivity3.MyHandler(this);
    public void begin() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = gt; i >= 0; i--) {
                    Message msg = new Message();
                    msg.arg1 = i;
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);//休眠1秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i(TAG," " + i);
                }
                //计时结束后
                Log.i(TAG,"计时结束");
                num.setText("请作答");
                EditText inputnum = findViewById(R.id.input3);
                inputnum.requestFocus();
                inputnum.setFocusableInTouchMode(true);
                inputnum.setInputType(InputType.TYPE_CLASS_NUMBER);
                Log.i(TAG,"可触碰");
            }
        }).start();
    }
}